import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {Link} from 'react-router-dom';
import {PAGINATION_OPTIONS, TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';
import {injectIntl} from './intl';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {pickingType, pickingListStatus, transportType, partDeliveryType} from '../Enum';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }

    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const colums = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.code',
                    defaultMessage: '备料单号'
                }),
                dataIndex: 'code',
                width: '100px',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'code' && this.props.sortedOrder,
                render: (text, record) => <Link to={routes.detail.format(record.id)} >{text}</Link>
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.transferOrderCode',
                    defaultMessage: '调拨单号'
                }),
                dataIndex: 'transferOrderCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.deliveryCode',
                    defaultMessage: '交货单号'
                }),
                dataIndex: 'deliveryCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partOrderCode',
                    defaultMessage: '订单号'
                }),
                dataIndex: 'partOrderCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dealerCode',
                    defaultMessage: '总代编号'
                }),
                dataIndex: 'dealerCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.brandCode',
                    defaultMessage: '品牌'
                }),
                dataIndex: 'brandCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partOrderTypeName',
                    defaultMessage: '订单类型'
                }),
                dataIndex: 'partOrderTypeName'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.transportType',
                    defaultMessage: '运输方式'
                }),
                dataIndex: 'transportType',
                render: text => conventEnumValueToString(transportType, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.pickingType',
                    defaultMessage: '拣货类型'
                }),
                dataIndex: 'pickingType',
                render: text => conventEnumValueToString(pickingType, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.itemCount',
                    defaultMessage: '品种数'
                }),
                dataIndex: 'itemCount'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.pickingQuantityCount',
                    defaultMessage: '拣货总数量'
                }),
                dataIndex: 'pickingQuantityCount'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.status',
                    defaultMessage: '备料状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(pickingListStatus, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.deliveryOrderStatus',
                    defaultMessage: '交货单状态'
                }),
                dataIndex: 'deliveryOrderStatus',
                render: text => conventEnumValueToString(partDeliveryType, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.createTime',
                    defaultMessage: '创建日期'
                }),
                dataIndex: 'createTime',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            }];
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={colums}
                    dataSource={this.props.list}
                    onChange={this.onChangeSort}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    locale={locale}
                    {...TABLE} />
            </div>
        );
    }
}

TablePanel.propTypes = {
    errorMessage: PropTypes.string.isRequired,
    list: PropTypes.array.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    list: getData(state),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option))
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
